package com.junzhiit.nengji.shop.web.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.bainaeco.framework.exception.InvalidArgumentException;
import com.bainaeco.framework.util.AssertUtil;
import com.junzhiit.nengji.model.enums.IsChainStatus;
import com.junzhiit.nengji.shop.service.GoodsService;
import com.junzhiit.nengji.shop.service.ShopService;
import com.junzhiit.nengji.shop.web.vo.ListVO;
import com.junzhiit.nengji.shop.web.vo.ResultVO;
import com.junzhiit.nengji.shop.web.vo.ShopVO;

import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/shop")
public class ShopController extends BaseController {

	@Autowired
	private ShopService shopService;
	
	@Autowired
	private GoodsService goodsService;

	@ApiOperation(value = "根据shopID查询店铺和店铺商品信息")
	@RequestMapping(value = "/{shopId}", method = RequestMethod.GET)
	public ResultVO getShopById(Integer pageNumber,Integer maxPageRow,String userId,@PathVariable String shopId,Integer status,Integer goodsSearchType) {
		AssertUtil.notNull(pageNumber, "pageNumber is required.");
		AssertUtil.notNull(maxPageRow, "maxPageRow is required.");
//		AssertUtil.notBlank(userId, "userId is required.");
		AssertUtil.notBlank(shopId, "shopId is required.");
		
		ResultVO result = new ResultVO();
		Map<String,Object> map=new HashMap<String,Object>();
		
		ShopVO shopVO=shopService.getById(userId,shopId);
		ListVO listVO=goodsService.findAll(userId,shopId,status,goodsSearchType,pageNumber,maxPageRow);
		if(shopVO!=null){
			map.put("shopInfo", shopVO);
		}
		if(listVO!=null){
			map.put("goodsList", listVO);
		}
		
		result.setData(map);
		return result;
	}
	
	@ApiOperation(value = "查询店铺详细信息")
	@RequestMapping(value = "/info", method = RequestMethod.GET)
	public ResultVO findShopById(String userId,String shopId) {
//		AssertUtil.notNull(userId, "userId is required.");
		AssertUtil.notBlank(shopId, "shopId is required.");
		ResultVO result=new ResultVO();
		result.setData(shopService.getById(userId, shopId));
		return result;
	}
	
	@ApiOperation(value = "查询在用户周边指定范围内的(自有小程序)店铺")
	@RequestMapping(value = "/within/distance", method = RequestMethod.GET)
	public ResultVO withinDistanceShop(String locations,String coordsys,Double distance){
		AssertUtil.notNull(locations, "locations is required.");
		AssertUtil.notNull(coordsys, "coordsys is required.");
		if(distance == null || distance <= 0 ){
			throw new InvalidArgumentException("distance is required.");
		}
		return shopService.withinDistanceShop(locations,coordsys,distance);
	}
	
	@ApiOperation(value = "查询(自有小程序)店铺,连锁品牌则查找在周边的店铺")
	@RequestMapping(value = "/around/shop", method = RequestMethod.GET)
	public ResultVO getShop(String locations,String coordsys,Integer isChain,String csId){
		AssertUtil.notBlank(csId, "csId is required.");
		if(isChain!=null){
			if(isChain == IsChainStatus.YES.toIntStatus()){
				AssertUtil.notBlank(locations, "locations is required.");
				AssertUtil.notBlank(coordsys, "coordsys is required.");
			}
		}else{
			throw new InvalidArgumentException("isChain is required.");
		}
		return shopService.aroundShop(locations, coordsys, isChain, csId);
	}
	
	@ApiOperation(value = "查询店铺,指定areaCityId下连锁品牌所有的店铺")
	@RequestMapping(value = "/chain/shop", method = RequestMethod.GET)
	public ResultVO getChainShop(String locations,String csId,String areaCityId){
		AssertUtil.notBlank(locations, "locations is required.");
		AssertUtil.notBlank(csId, "csId is required.");
		
		return shopService.chainShop(locations, csId, areaCityId);
	}
}
